<template>
	<view class="page" :style="{ height: height + 'px'}" style="background-color: #000;">
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
        options: {},
				height: 0,
        passive: false
			}
		},
    onBackPress () {
      return !this.passive
    },
		onLoad(options) {
      this.options = options
			setTimeout(() => {
				uni.$emit("callMsg", options);
			}, 500);
		    this.bindEvent();
			this.height = uni.getSystemInfoSync().windowHeight;
      console.log(this.height);
		},
		onUnload() {
      if (!this.passive) {
        uni.$emit("cancelled", {
          calltype: this.options.calltype
        });
      }
			uni.$off('rejected');
			uni.$off('timeout');
			uni.$off('cancelled');
      plus.screen.lockOrientation("landscape-primary");
		},
		methods: {
			bindEvent() {
				// 主叫方
				uni.$on('rejected', (e) => {
          uni.showToast({
            title: '对方拒绝！',
            icon: 'none'
          })
          this.passive = true
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				})
				// 主叫方
				uni.$on('timeout', (e) => {
					console.log('邀请取消', e)
					if (!e) {
						uni.showToast({
						  title: '由于对方长时间未应答，呼叫已取消！',
						  icon: 'none'
						})
            let obj = {
              roomid: '',
              tencent_user_id: this.options.user
            }
            console.log(obj);
            uni.$http.rejectService(obj).then(res => {
              console.log('rejectService:',res);
            })
            this.passive = true
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					}
				})

				//被叫方
				uni.$on('cancelled', (e) => {
					console.log('邀请取消', e)
          let obj = {
            roomid: '',
            tencent_user_id: this.options.user
          }
          uni.$http.rejectService(obj).then(res => {
            console.log('rejectService:',res);
          })
          this.passive = true
					uni.navigateBack();
				})
			}
		}
	}
</script>

<style lang="scss">
</style>
